<?php $this->load->view('common/default_header.php');?>

<div class="container">
	<div class="row">
		<?php $this->load->view('common/index_nav.php');?>
		<h3>行情</h3>
		<table class="table table-condensed table-bordered">
			<tr>
		  		<th>产品名称</th>
		  		<th>卖一</th>
		  		<th>中间价</th>
		  		<th>买一</th>
		  		<th>昨收</th>
		  		<th>涨跌</th>
		  		<th>涨跌幅</th>
		  		<th>更新时间</th>
		  	</tr>
			<?php 
			if(!empty($productArr)) foreach($productArr as $stype=>$productType){
			?>
			<tr>
		  		<th colspan="8"><?php echo $stype?></th>
		  	</tr>
		  	<?php 
				foreach($productType as $name=>$product){
		 	?>
		  	<tr>
		  		<td><a href="/cfd?pid=<?php echo $product['pid'];?>"><?php echo $product['desc'];?> (<?php echo $product['symbol'];?>)</a></td>
		  		<td class="success" id="<?php echo $name;?>_ASK_PRICE">--</td>
		  		<td class="warning" id="<?php echo $name;?>_LAST_PRICE">--</td>
		  		<td class="info" id="<?php echo $name;?>_BID_PRICE">--</td>
		  		<td id="<?php echo $name;?>_CLOSE_PRICE">--</td>
		  		<td id="<?php echo $name;?>_UD">--</td>
		  		<td id="<?php echo $name;?>_RANGE">--</td>
		  		<td id="<?php echo $name;?>_exchange_time">--</td>
		  	</tr>
		  	<?php 
		  		}
		  	}
		  	?>
		</table>

		<h3>当前持仓</h3>
		<table class="table table-condensed table-bordered">
			<tr>
		  		<th>产品名称</th>
		  		<th>计价单位</th>
		  		<th>类型</th>
		  		<th>均价</th>
		  		<th>持仓量</th>
		  		<th>冻结量</th>
		  		<th>保证金</th>
		  		<th>盈亏</th>
		  		<th>盈亏率</th>
		  		<th>佣金</th>
		  		<th>止损</th>
		  		<th>爆仓指数</th>
		  		<th>平仓指数</th>
		  		<th>平仓量</th>
		  		<th>操作</th>
		  	</tr>
			<?php 
			if(!empty($positionArr)) foreach($positionArr as $position){
			?>
		  	<tr>
		  		<td><?php echo $position['name'];?></td>
		  		<td><?php echo $position['dtype'];?></td>
		  		<td><?php echo ($position['bs']=='bid')?"买多":"卖空";?>(<?php echo $position['times'];?>)</td>
		  		<td><?php echo del0($position['price']);?></td>
		  		<td><?php echo $position['total'];?></td>
		  		<td><?php echo $position['block_num'];?></td>
		  		<td><?php echo $position['margin'];?></td>
		  		<td><?php echo $position['yk'];?></td>
		  		<td><?php echo $position['ykl'];?>%</td>
		  		<td><?php echo $position['fee'];?></td>
		  		<td><?php echo $position['stop_lose'];?></td>
		  		<td><?php echo $position['bao_price'];?></td>
		  		<td>--</td>
		  		<td>-</td>
		  		<td>--</td>
		  	</tr>
		  	<?php 
		  	}
		  	?>
		</table>
	</div>
</div>

<?php $this->load->view('common/default_footer.php');?>

<script type="text/javascript">
var tmpData = {};
var socket = io.connect('<?php echo $socket_url;?>',{'max reconnection attempts':'Infinity','reconnectiondelay':5,'reconnection limit':3000});
socket.on('connect', function() {
	<?php 
	if(!empty($productArr)) foreach($productArr as $productType){
		foreach($productType as $name=>$product){
		$tradeKey = $name."_TRADE";
 	?>
	socket.on('<?php echo $tradeKey?>', function(data) {
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].LAST_PRICE!=data.LAST_PRICE){
			$("#<?php echo $name;?>_LAST_PRICE").html(data.LAST_PRICE);
			flash("#<?php echo $name;?>_LAST_PRICE","danger","warning");
		}
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].BID_PRICE!=data.BID_PRICE){
			$("#<?php echo $name;?>_BID_PRICE").html(data.BID_PRICE);
			flash("#<?php echo $name;?>_BID_PRICE","danger","info");
		}
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].ASK_PRICE!=data.ASK_PRICE){
			$("#<?php echo $name;?>_ASK_PRICE").html(data.ASK_PRICE);
			flash("#<?php echo $name;?>_ASK_PRICE","danger","success");
		}
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].CLOSE_PRICE!=data.CLOSE_PRICE){
			$("#<?php echo $name;?>_CLOSE_PRICE").html(data.CLOSE_PRICE);
		}
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].UD!=data.UD){
			$("#<?php echo $name;?>_UD").html(data.UD);
			flash("#<?php echo $name;?>_UD");
		}
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].RANGE!=data.RANGE){
			$("#<?php echo $name;?>_RANGE").html(data.RANGE+"%");
			flash("#<?php echo $name;?>_RANGE");
		}
		if(tmpData['<?php echo $tradeKey?>']==undefined ||tmpData['<?php echo $tradeKey?>'].exchange_time!=data.exchange_time){
			$("#<?php echo $name;?>_exchange_time").html(data.exchange_time); 
		}
		if(data.UD>0){
        	$("#<?php echo $name;?>_UD").removeClass("red").addClass("green");
        	$("#<?php echo $name;?>_RANGE").removeClass("red").addClass("green");
        	
        }else{
        	$("#<?php echo $name;?>_UD").removeClass("green").addClass("red");
        	$("#<?php echo $name;?>_RANGE").removeClass("green").addClass("red");
        }
		tmpData['<?php echo $tradeKey?>'] = data;
	});
	<?php }}?>
	socket.on('disconnect', function(data) {

	});
	socket.on('error', function(data) {
		return;
	});
});

function flash(ele,newClass,oldClass){
	if(newClass == undefined) newClass='danger';
	if(oldClass == undefined) oldClass='';
	$(ele).removeClass(oldClass).addClass(newClass);
	setTimeout(function(){
		$(ele).removeClass(newClass).addClass(oldClass);
	},200);
}

</script>
</body>
</html>